/**
 * 
 */
package com.wqr.it.ecwdr.client.util;

import java.util.ArrayList;
import java.util.List;

import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.springframework.util.StringUtils;

/**
 * 基础解析器
 * @author adinos
 *
 */
public class BaseParser {
	private int pageSize = 15;
	
	public String getText(Object[] t) {
		if (t != null && t.length > 0) {
			return ((TagNode)t[0]).getText().toString();
		} else {
			return null;
		}
	}
	
	public static String getText(TagNode node, String selector) throws XPatherException {
		Object[] tagNodes = node.evaluateXPath(selector);
		if (tagNodes == null || tagNodes.length == 0) {
			return null;
		} else {
			return ((TagNode)tagNodes[0]).getText().toString().trim();
		}
	}
	
	public static String getText(TagNode node, String selector, String defValue, String attr) throws XPatherException {
		Object[] tagNodes = node.evaluateXPath(selector);
		if (tagNodes == null || tagNodes.length == 0) {
			return null;
		} else {
			return ((TagNode)tagNodes[0]).getAttributeByName(attr);
		}
	}
	
	public String getText(TagNode node, String selector, String defValue) throws XPatherException {
		String value = getText(node, selector);
		if (value == null) {
			value = defValue;
		}
		return value;
	}
	
	public String getTextNotEmpty(TagNode node, String selector, String defValue) throws XPatherException {
		String value = getText(node, selector);
		if (value == null || value.trim().equals("")) {
			value = defValue;
		}
		return value;
	}
	
	public static TagNode getElement(TagNode node, String selector) throws XPatherException {
		Object[] tagNodes = node.evaluateXPath(selector);
		if (tagNodes == null || tagNodes.length == 0) {
			return null;
		} else {
			return ((TagNode)tagNodes[0]);
		}
	}
	
	public List<String> getTextList(TagNode node, String selector) throws XPatherException {
		Object[] tagNodes = node.evaluateXPath(selector);
		if (tagNodes == null || tagNodes.length == 0) {
			return null;
		} else {
			List<String> list = new ArrayList<String>();
			for (int i = 0; i < tagNodes.length; i++) {
				list.add(((TagNode)tagNodes[i]).getText().toString().trim());
			}
			return list;
		}
	}
	
	public static List<TagNode> getElementList(TagNode node, String selector) throws XPatherException {
		Object[] tagNodes = node.evaluateXPath(selector);
		if (tagNodes == null || tagNodes.length == 0) {
			return null;
		} else {
			List<TagNode> list = new ArrayList<TagNode>();
			for (int i = 0; i < tagNodes.length; i++) {
				list.add(((TagNode)tagNodes[i]));
			}
			return list;
		}
	}
	

	public static String getAttrText(String selector, TagNode node, String attrName) throws XPatherException {
		String text = null;
		if (node != null && !StringUtils.isEmpty(selector) && !StringUtils.isEmpty(attrName)) {
			node = getElement(node, selector);
			if (node != null) {
				text = node.getAttributeByName(attrName);
			}
		}
		return text;
	}

	public static String getAttrText(TagNode node, String selector, String attrName) throws XPatherException {
		String text = null;
		if (node != null && !StringUtils.isEmpty(selector) && !StringUtils.isEmpty(attrName)) {
			node = getElement(node, selector);
			if (node != null) {
				text = node.getAttributeByName(attrName);
			}
		}
		return text;
	}

	public static String getAttrText(TagNode node, String attrName) throws XPatherException {
		String text = null;
		if (node != null) {
			text = node.getAttributeByName(attrName);
		}
		return text;
	}


	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return this.pageSize;
	}
}
